<template>
  <!-- 添加或修改部门对话框 -->
  <el-dialog :title="title" :visible.sync="dialogVisible" width="600px" append-to-body>
    <el-form ref="form" :model="form" :rules="rules" label-width="80px">
      <el-row>
        <el-col :span="24" v-if="form.parentId !== 0">
          <el-form-item label="上级部门" prop="parentId">
            <treeselect v-model="form.parentId" :options="deptOptions" :normalizer="normalizer" placeholder="选择上级部门" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="部门名称" prop="deptName">
            <el-input v-model="form.deptName" placeholder="请输入部门名称" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="显示排序" prop="orderNum">
            <el-input-number v-model="form.orderNum" controls-position="right" :min="0" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="负责人" prop="leader">
            <el-input v-model="form.leader" placeholder="请输入负责人" maxlength="20" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="联系电话" prop="phone">
            <el-input v-model="form.phone" placeholder="请输入联系电话" maxlength="11" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="邮箱" prop="email">
            <el-input v-model="form.email" placeholder="请输入邮箱" maxlength="50" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="部门状态">
            <el-radio-group v-model="form.status">
                <el-radio v-for="dict in dict.type.sys_normal_disable" :key="dict.value"
                  :label="dict.value">{{dict.label}}</el-radio>
              </el-radio-group>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitForm">确 定</el-button>
      <el-button @click="cancel">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { handleTree } from "@/utils/ruoyi";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import Treeselect from "@riophae/vue-treeselect";
import {
  listDept,
  getDept,
  addDept,
  updateDept,
  listDeptExcludeChild
} from "@/api/dept";
export default {
  name: "CreatGrade",
  props: {
    'user': Object
  },
  components: {
    Treeselect
  },
  dicts: ['sys_normal_disable'],
  data() {
    return {
      dialogVisible: false,
      loading: false,
      // 部门树选项
      deptOptions: [],
      // 表单参数
      form: {},
      title: "",
      // 表单校验
      rules: {
        parentId: [{
          required: true,
          message: "上级部门不能为空",
          trigger: "blur"
        }],
        deptName: [{
          required: true,
          message: "部门名称不能为空",
          trigger: "blur"
        }],
        orderNum: [{
          required: true,
          message: "显示排序不能为空",
          trigger: "blur"
        }],
        email: [{
          type: "email",
          message: "请输入正确的邮箱地址",
          trigger: ["blur", "change"]
        }],
        phone: [{
          pattern: /^1[3|4|5|6|7|8|9][0-9]\d{8}$/,
          message: "请输入正确的手机号码",
          trigger: "blur"
        }]
      }
    }
  },
  methods: {
    //修改部门树
    info(deptId, parentId) {
      this.loading = true
      console.log(deptId);
      if (deptId) {
        this.handleUpdate(deptId)
      } else {
        this.handleAdd(parentId);
      }
    },
    handleClose() {
      this.dialogVisible = false;
      // this.user = Object.assign({}, '')
    },

    /** 修改按钮操作 */
    handleUpdate(deptId) {
      this.reset();
      getDept(deptId).then(response => {
        this.form = response;
        this.open = true;
        this.title = "修改部门";
        listDeptExcludeChild(deptId).then(resp => {
          this.deptOptions = handleTree(resp, "deptId");
          if (this.deptOptions.length == 0) {
            const noResultsOptions = {
              deptId: this.form.parentId,
              deptName: this.form.parentName,
              children: []
            };
            this.deptOptions.push(noResultsOptions);
          }
          this.dialogVisible = true;
        });
      });
    },

    /** 新增按钮操作 */
    handleAdd(parentId) {
      this.reset();
      if (parentId != undefined) {
        this.form.parentId = parentId;
      }else {
        this.form.parentId = 0;
      }
      this.open = true;
      this.title = "添加部门";
      listDept().then(response => {
        this.deptOptions = handleTree(response.list, "deptId");
        this.dialogVisible = true;
      });
    },

    // 表单重置
    reset() {
      this.form = {
        deptId: undefined,
        parentId: undefined,
        deptName: undefined,
        orderNum: undefined,
        leader: undefined,
        phone: undefined,
        email: undefined,
        status: "0"
      };
      this.resetForm("form");
    },

    resetForm(formName) {
      this.dialogVisible = false;
      this.$nextTick(() => {
        if (this.$refs[formName]) {
          this.$refs[formName].resetFields();
        };
      })
    },

    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.deptId != undefined) {
            updateDept(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.dialogVisible = false;
              this.$emit("refresh");
            });
          } else {
            addDept(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.dialogVisible = false;
              this.$emit("refresh");
            });
          }
        }
      });
    },

    // 取消按钮
    cancel() {
      this.dialogVisible = false;
      this.reset();
    },

    /** 转换部门数据结构 */
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.deptId,
        label: node.deptName,
        children: node.children
      };
    },

  }
}
</script>

<style scoped></style>
